package com.imut.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imut.oa.common.ErrorCode;
import com.imut.oa.common.PageResult;
import com.imut.oa.exception.BusinessException;
import com.imut.oa.mapper.DepartmentMapper;
import com.imut.oa.mapper.UserMapper;
import com.imut.oa.pojo.dto.DepartmentAddDTO;
import com.imut.oa.pojo.dto.DepartmentPageQueryDTO;
import com.imut.oa.pojo.entity.Departments;
import com.imut.oa.pojo.entity.Leave;
import com.imut.oa.pojo.entity.User;
import com.imut.oa.pojo.vo.DepartmentVO;
import com.imut.oa.pojo.vo.LeaveVO;
import com.imut.oa.service.DepartmentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departments>
        implements DepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 部门分页查询
     * @param departmentPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DepartmentPageQueryDTO departmentPageQueryDTO) {
        Page<DepartmentVO> page = new Page<>(departmentPageQueryDTO.getPage(), departmentPageQueryDTO.getPageSize());
        // 调用自定义的联表分页查询
        Page<DepartmentVO> voPage = departmentMapper.selectDepartmentPage(page, departmentPageQueryDTO);
        System.out.println(voPage);
        return new PageResult(voPage.getTotal(), voPage.getRecords());
    }


    /**
     * 部门添加
     * @param
     * @return
     */
    @Override
    public void save(DepartmentAddDTO departmentAddDTO) {
        if (departmentAddDTO.getDepartmentName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"部门名称不能为空");
        }
        Departments build = Departments.builder()
                .departmentName(departmentAddDTO.getDepartmentName())
                .departmentDescription(departmentAddDTO.getDepartmentDescription())
                .isDelete(0)
                .build();
        //创建条件对象，不设置任何条件，相当于查询所以
        QueryWrapper<Departments> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Departments> departmentName = queryWrapper.eq("departmentName", departmentAddDTO.getDepartmentName());
        // 执行查询
        Departments departments = departmentMapper.selectOne(queryWrapper);
        if (departments != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"部门已存在");
        }
        departmentMapper.customInsert(build);


    }

    @Override
    public void updateDepartment(DepartmentAddDTO departmentAddDTO) {
        if (departmentAddDTO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"部门id不能为空");
        }
        if (departmentAddDTO.getDepartmentName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"部门名称不能为空");
        }
        Departments build = Departments.builder()
                .id(departmentAddDTO.getId())
                .departmentName(departmentAddDTO.getDepartmentName())
                .departmentDescription(departmentAddDTO.getDepartmentDescription())
                .isDelete(0)
                .build();

        departmentMapper.updateByDepartmentId(build);
    }

    @Override
    public void removeDepartmentById(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"部门id不能为空");
        }
        Long userCount = userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getDepartmentId, id));
        if (userCount > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"部门下有员工，不能删除");
        }
        UpdateWrapper<Departments> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("isDelete",1);
        departmentMapper.update(updateWrapper);
    }
}
